<div class="buying-services">
    <h3 class="popup-title">Условия акции</h3>
    <p class="global-warning">
        Только до <strong><?php echo (int) date('d', time() + 3600 * 24), ' ', Yii::app()->locale->getMonthName(date('n', time() + 3600 * 24)); ?></strong><br>
        <?php echo urldecode($_GET['service']); ?>&nbsp;&nbsp;<strong><?php echo $_GET['bim']; ?></strong><br>всего за <?php
        echo '<span class="chz">';
        echo Controller::numberFormat($_GET['priceOld']) . '&nbsp;';
        echo '</span>';
        ?> <strong><?php echo $_GET['price']; ?></strong> рублей!
        <!--            Заполните форму сейчас и наши
                    менеджеры свяжутся с вами в течение 15 рабочих минут            -->
    </p>
    <div class="buying-services__form">
        <?php
        $form = $this->beginWidget('CActiveForm', array(
            'id' => 'buying-form-2',
            'enableClientValidation' => true,
            'clientOptions' => array(),
            'htmlOptions' => array('class' => 'buy__service__ajax_form'),
        ));
        ?>
        <div style="display: none">
            <?php echo $form->error($model, 'userName'); ?>
            <?php echo $form->error($model, 'userPhone'); ?>
            <?php echo $form->error($model, 'userEmail'); ?>
        </div>
        <fieldset>
            <div class="type-text">
                <?php echo $form->textField($model, 'userName', array('placeholder' => 'Ваше имя')); ?>	
            </div>
        </fieldset>        
        <fieldset>
            <div class="type-text">
                <?php echo $form->textField($model, 'userPhone', array('placeholder' => 'Ваш телефон', 'class' => 'masked-phone')); ?>				
            </div>
        </fieldset>
        <fieldset style="display: block;">
            <div class="type-text">
                <?php echo $form->textField($model, 'userEmail', array('placeholder' => 'Ваш E-mail')); ?>	
            </div>
        </fieldset>
        <fieldset style="display: none;">
            <div class="select">
                <?php
                $model->city = Yii::app()->params['regions'][Yii::app()->session['userRegion']['id']]['regionTitle'];
                $uRegions = [];
                foreach (Yii::app()->params['regions'] as $region) {
                    $uRegions[$region['regionTitle']] = $region['regionTitle'];
                }
                ?>
                <?php echo $form->dropdownList($model, 'city', $uRegions); ?>
            </div>
        </fieldset>
        <fieldset class="choice-courier" id="choice-courier">
            <div class="checkbox<?php echo ($model->courier == 1 ? ' open ' : ''); ?>">
                <?php echo $form->checkBox($model, 'courier', array('id' => 'need-courier-address', 'value' => 1, 'uncheckValue' => 0)); ?>					
                <label for="need-courier-address">
                    <span class="checkbox__box">
                        <span class="checkbox__box__inner">&nbsp;</span>
                    </span>    
                    <span class="checkbox__text">Нужен бесплатный курьер</span>
                </label>
                <div class="wrap-bg">
                    <div class="type-text">
                        <?php echo $form->textField($model, 'userAddress', array('placeholder' => 'Введите ваш адрес')); ?>
                    </div>
                </div>
            </div>
        </fieldset>	
        <fieldset>
            <div class="type-submit">
                <?php echo CHtml::submitButton('Заказать'); ?>				
            </div> 
        </fieldset>
        <?php $this->endWidget(); ?>
    </div>	
    <p class="global-warning">Заполните форму сейчас и наши
        менеджеры свяжутся с вами в течение 15 рабочих минут.</p>
</div>
<script type="text/javascript">

    $("#buyServiceForm_userPhone").mask("+7 (999) 999-99-99");

    $('#choice-courier .checkbox label').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    var action = localStorage.getItem("mobi03.buy_action");

    if (action == '')
    {
        $('.fancybox-close').trigger('click');
    }
    $('form#buying-form-2').attr("action", action);

    localStorage.setItem("mobi03.buy_action", "");

    $('#buying-form-2').yiiactiveform(
            {
                'validateOnSubmit': true,
                'afterValidate': function (form, data, hasError)
                {
                    if (!hasError)
                    {
                        var courier = "without courier";
                        var checkbox = $("form#buying-form-2 div.checkbox");
                        if (checkbox.hasClass("open"))
                        {
                            courier = "courier";
                        }

                        var cur = $('#curer').find('input[type="radio"]:checked');
                        var status = cur.attr('id') == 'status' ? 'status' : 'new client';
                        _gaq.push(["_trackEvent", "Order-online-from-price-promo", courier, window.location.href]);


                        $.ajax({
                            type: "POST",
                            url: form[0].action,
                            data: $(form).serialize(),
                            success: function (ret)
                            {
                                console.log(data);
                                $('.fancybox-close').trigger('click');
                                var data = JSON.parse(ret);
                                mobi03.showMessage(data.message.title, data.message.desc);
                            }
                        });
                    }
                },
                'attributes': [{
                        'id': 'buyServiceForm_userName',
                        'inputID': 'buyServiceForm_userName',
                        'errorID': 'buyServiceForm_userName_em_',
                        'model': 'buyServiceForm',
                        'name': 'userName',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '')
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Name\u00bb.");
                            }
                        }
                    },
                    {'id': 'buyServiceForm_userPhone',
                        'inputID': 'buyServiceForm_userPhone',
                        'errorID': 'buyServiceForm_userPhone_em_',
                        'model': 'buyServiceForm',
                        'name': 'userPhone',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '')
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Phone\u00bb.");
                            }

                        }
                    },
                    {'id': 'buyServiceForm_userAddress',
                        'inputID': 'buyServiceForm_userAddress',
                        'errorID': 'buyServiceForm_userAddress_em_',
                        'model': 'buyServiceForm',
                        'name': 'userPhone',
                        'enableAjaxValidation': false,
                        'clientValidation': function (value, messages, attribute)
                        {
                            if (jQuery.trim(value) == '' && $("form#buying-form div.checkbox").hasClass("open"))
                            {
                                messages.push("\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u00abUser Address\u00bb.");
                            }

                        }
                    }
                ],
                'errorCss': 'error'
            });
</script>